package org.moonshiner.controller.security;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

public abstract class FileEncryptor {

	public abstract void doCrypt(String password, InputStream in, OutputStream out,
			int cipherMode);

	protected void doCopy(InputStream in, OutputStream out) {
		byte[] buffer = new byte[128];
		int length = 0;
		try {
			while ((length = in.read(buffer)) >= 0) {
				out.write(buffer, 0, length);
			}
		} catch (IOException exception) {

		} finally {
			try {
				out.flush();
				out.close();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	protected Cipher getChiper(String algorithm) {
		Cipher cipher = null;
		try {
			cipher = Cipher.getInstance(algorithm);
		} catch (NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (NoSuchPaddingException e1) {
			e1.printStackTrace();
		}
		return cipher;
	}

}
